﻿namespace LevelEditors
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.CompilerServices;
    using System.Text.RegularExpressions;
    using UnityEngine;
    using UnityEngine.Serialization;
    using UnityEngine.UI;

    public class UIPrefabBrowser : MonoBehaviour
    {
        private List<UIElement> elements = new List<UIElement>();
        private ToggleGroup group;
        public static UIPrefabBrowser Instance;
        private string m_category;
        [SerializeField, FormerlySerializedAs("m_contentPrefab")]
        private UIElement m_elementPrefab;
        [SerializeField]
        private InputField m_filterInputField;
        [SerializeField]
        private Transform m_group;
        [SerializeField]
        private ScrollRect m_scrollRect;
        [SerializeField]
        private UIElement m_selectedPrefabContent;

        private void Awake()
        {
            this.m_category = PlayerPrefs.GetString("UIPrefabBrowser.category");
            Instance = this;
            this.group = this.m_group.GetComponent<ToggleGroup>();
            LevelEditor.Instance.onSelectPrefab += delegate {
                <Awake>c__AnonStorey3D storeyd = new <Awake>c__AnonStorey3D {
                    <>f__this = this,
                    prefab = AssetManager.GetPrefabAsset(LevelEditor.Instance.selectedPrefabName)
                };
                this.m_selectedPrefabContent.label = storeyd.prefab.localizedName;
                this.m_selectedPrefabContent.labelAlignment = TextAnchor.LowerLeft;
                this.m_selectedPrefabContent.preview = this.GetModel(storeyd.prefab.gameObject);
                this.m_selectedPrefabContent.onRightClick = new Action(storeyd.<>m__2E);
            };
            this.m_filterInputField.onValueChanged.AddListener(t => this.Refresh());
        }

        private string GetCatetory(string fullName)
        {
            return (!fullName.Contains("/") ? "^[^/]*$" : fullName.Substring(0, fullName.LastIndexOf("/") + 1));
        }

        private UIElement GetContent(string name)
        {
            <GetContent>c__AnonStorey3E storeye = new <GetContent>c__AnonStorey3E {
                name = name
            };
            return this.elements.Find(new Predicate<UIElement>(storeye.<>m__27));
        }

        private GameObject GetModel(GameObject prefab)
        {
            try
            {
                return prefab.gameObject.GetComponent<Model>().GetPrefab().gameObject;
            }
            catch (Exception)
            {
            }
            try
            {
                return prefab.transform.Find("Model").gameObject;
            }
            catch (Exception)
            {
            }
            return prefab;
        }

        private void HandlePrefabRightClick(Asset prefab)
        {
            <HandlePrefabRightClick>c__AnonStorey41 storey = new <HandlePrefabRightClick>c__AnonStorey41 {
                prefab = prefab
            };
            UIGenericMenu.Clear();
            if (File.Exists(storey.prefab.modelFileName))
            {
                UIGenericMenu.Add("编辑模型", false, new Action(storey.<>m__2A));
            }
            if (File.Exists(storey.prefab.dataFileName) && !storey.prefab.dataFileName.ContainsIgnoreCase("/Templates/"))
            {
                UIGenericMenu.Add("编辑数据", false, new Action(storey.<>m__2B));
            }
            storey.scriptFileName = Path.ChangeExtension(storey.prefab.modelFileName, ".cs");
            if (File.Exists(storey.scriptFileName))
            {
                UIGenericMenu.Add("编辑脚本", false, new Action(storey.<>m__2C));
            }
            storey.folderName = !File.Exists(storey.prefab.modelFileName) ? string.Empty : Path.GetDirectoryName(storey.prefab.modelFileName);
            if (Directory.Exists(storey.folderName))
            {
                UIGenericMenu.Add("打开文件夹", false, new Action(storey.<>m__2D));
            }
            UIGenericMenu.Show();
        }

        private bool IsMatch(string input, string pattern)
        {
            try
            {
                return Regex.IsMatch(input, pattern);
            }
            catch (Exception)
            {
            }
            return false;
        }

        private void OnDestroy()
        {
            PlayerPrefs.SetString("UIPrefabBrowser.category", this.m_category);
        }

        public void Refresh()
        {
            <Refresh>c__AnonStorey3F storeyf = new <Refresh>c__AnonStorey3F {
                <>f__this = this
            };
            if (!string.IsNullOrEmpty(this.category) && LevelEditor.Instance.hasLevelSelected)
            {
                storeyf.prefabs = AssetManager.GetAllPrefabAssets().Where<Asset>(new Func<Asset, bool>(storeyf.<>m__28)).ToArray<Asset>();
                this.m_group.localPosition = this.m_group.localPosition.SetY(0f);
                UIUtility.RefreshElements<UIElement>(this.elements, storeyf.prefabs.Length, this.m_elementPrefab, this.m_group, new Action<UIElement, int>(storeyf.<>m__29));
            }
        }

        private void Start()
        {
            this.Refresh();
        }

        public string category
        {
            get
            {
                return this.m_category;
            }
            set
            {
                string category = this.m_category;
                this.m_category = value;
                if (category != value)
                {
                    this.Refresh();
                }
            }
        }

        [CompilerGenerated]
        private sealed class <Awake>c__AnonStorey3D
        {
            internal UIPrefabBrowser <>f__this;
            internal Asset prefab;

            internal void <>m__2E()
            {
                this.<>f__this.HandlePrefabRightClick(this.prefab);
            }
        }

        [CompilerGenerated]
        private sealed class <GetContent>c__AnonStorey3E
        {
            internal string name;

            internal bool <>m__27(UIElement x)
            {
                return (x.name == this.name);
            }
        }

        [CompilerGenerated]
        private sealed class <HandlePrefabRightClick>c__AnonStorey41
        {
            internal string folderName;
            internal Asset prefab;
            internal string scriptFileName;

            internal void <>m__2A()
            {
                Application.OpenURL(this.prefab.modelFileName);
            }

            internal void <>m__2B()
            {
                Application.OpenURL(this.prefab.dataFileName);
            }

            internal void <>m__2C()
            {
                Application.OpenURL(this.scriptFileName);
            }

            internal void <>m__2D()
            {
                Application.OpenURL(this.folderName);
            }
        }

        [CompilerGenerated]
        private sealed class <Refresh>c__AnonStorey3F
        {
            internal UIPrefabBrowser <>f__this;
            internal Asset[] prefabs;

            internal bool <>m__28(Asset x)
            {
                return x.category.Contains(this.<>f__this.category);
            }

            internal void <>m__29(UIElement c, int i)
            {
                <Refresh>c__AnonStorey40 storey = new <Refresh>c__AnonStorey40 {
                    <>f__ref$63 = this,
                    c = c,
                    prefab = this.prefabs[i]
                };
                storey.c.group = this.<>f__this.group;
                storey.c.index = i;
                storey.c.name = storey.prefab.name;
                storey.c.label = storey.prefab.localizedName;
                storey.c.SetPreviewLazy(new Func<GameObject>(storey.<>m__2F));
                storey.c.pivot = (Vector2) (Vector2.one * 0.5f);
                storey.c.labelAlignment = TextAnchor.LowerLeft;
                storey.c.target = storey.prefab;
                storey.c.isOn = LevelEditor.Instance.selectedPrefabName == storey.c.name;
                storey.c.onLeftClick = new Action(storey.<>m__30);
                storey.c.onRightClick = new Action(storey.<>m__31);
            }

            private sealed class <Refresh>c__AnonStorey40
            {
                internal UIPrefabBrowser.<Refresh>c__AnonStorey3F <>f__ref$63;
                internal UIElement c;
                internal Asset prefab;

                internal GameObject <>m__2F()
                {
                    return this.<>f__ref$63.<>f__this.GetModel(this.prefab.gameObject);
                }

                internal void <>m__30()
                {
                    LevelEditor.Instance.selectedPrefabName = this.c.name;
                }

                internal void <>m__31()
                {
                    this.<>f__ref$63.<>f__this.HandlePrefabRightClick(this.prefab);
                }
            }
        }
    }
}

